<?php
// +----------------------------------------------------------------------
// | YFCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.rainfer.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: rainfer <81818832@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\controller\Common;
use app\admin\model\AuthRule;

class Base extends Common 
{
    public function _initialize()
    {
        parent::_initialize();
        if(!$this->check_admin_login()) $this->redirect('admin/Login/login');//未登录
        //更新上次请求时间
        session('last_action_time',time());
        
        $auth = new AuthRule;
        $id_curr = $auth->get_url_id();
        
        if (!$auth->check_auth($id_curr)) {
            $this->error('没有权限', url('admin/Index/index'));
        }
        //记录日志
        $log = [
            'uid' => session('userinfo.id'),
            'request_id' => $id_curr,
            'create_time' => time()   
        ];
        $param = input();
        if(!empty($param)) $param = json_encode($param);
        $log['request_param'] = $param;
        db('action_log')->insert($log);
        
                
        //获取有权限的菜单tree
        $menus = $auth->get_admin_menus();
        $this->assign('menus',$menus);
        //当前方法倒推到顶级菜单ids数组
        $menus_curr = $auth->get_admin_parents($id_curr);
        $this->assign('menus_curr',$menus_curr);
        //取当前操作菜单父节点下菜单 当前菜单id(仅显示状态)
        //$menus_child = $auth->get_admin_parent_menus($id_curr);
        $new_menus_child = array();
        foreach($menus as $k => $v){
            foreach ($v['_child'] as $a => $b){
                if($id_curr==$b['id']){
                    if(isset($v['_child']) && !empty($v['_child'])){
                        $new_menus_child = $v['_child'];
                    }
                }
            }
        }
        
        //判断用户是否有清除缓存权限
        $is_show_clear = '';
        $auth_id = $auth->get_url_id('admin/Sys/clear');
        if ($auth->check_auth($auth_id)) {
           $is_show_clear = 1;
        }

        $this->assign('menus_child',$new_menus_child);
        $this->assign('is_show_clear', $is_show_clear);
        $this->assign('id_curr',$id_curr);

    }

    public function delFile($path)
    {
        if (file_exists($path)){
            unlink($path);
        }
    }
}